const path = require('path');
const favicon = require('serve-favicon');
const compress = require('compression');
const helmet = require('helmet');
const cors = require('cors');
const logger = require('./logger');
const memory = require('feathers-memory');
const feathers = require('@feathersjs/feathers');
const configuration = require('@feathersjs/configuration');
const socketio = require('@feathersjs/socketio');
const express = require('@feathersjs/express');

const middleware = require('./middleware');
const services = require('./services');
const appHooks = require('./app.hooks');
const channels = require('./sockets/channels');

const sequelize = require('./sequelize');
const mongodb = require('./mongodb');
const authentication = require('./authentication');
const app = express(feathers());
// api
const coinApi = require('./tasks/coinApi')

app.configure(configuration());
// Enable security, CORS, compression, favicon and body parsing
app.use(helmet());
app.use(cors());
app.use(compress());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(favicon(path.join(app.get('public'), 'favicon.ico')));
// Host the public folder
app.use('/', express.static(app.get('public')));

// Set up Plugins and providers
app.configure(express.rest());
app.configure(socketio());

app.configure(sequelize);
app.configure(mongodb)

// Configure other middleware (see `middleware/index.js`)
app.configure(middleware);
// app.configure(authentication);
// Set up our services (see `services/index.js`)
app.configure(services);

// Set up event channels (see channels.js)
app.configure(channels);
app.globalData = {}
// require('./coinSocketService')(app)
require('./sockets/quoteSocket')(app)
require('./sockets/mdcSocket')(app)
/* coinApi.getSymbols()
  .then(result => {
    let i = 0;
    setInterval(() => {
      if (i < result.length) {
        app.service('symbols')
          .create(result[i]);
        i++;
      }
    }, 10);
  })
  .catch(err => {
    debugger;
  }); */
// Configure a middleware for 404s and the error handler
const base = 'EOS';
const quote = 'USDT'
/* coinApi.getExchanges()
  .then(result => {
    result.forEach(item => {
      app.service('exchange')
      .create(item)
    })
  }) */
/*coinApi.getPeriods()
  .then(result => {
    result.forEach(item => {
      app.service('periods')
        .create(item);
    })
  })*/
/*   coinApi.getAssets()
  .then(result => {
    var newResult = []
    for(let i=0,len=result.length;i<len;i+=1000){
      newResult.push(result.slice(i,i+1000));
    }
    let j = 0;
    setInterval(() => {
      if (j < newResult.length) {
        app.service('asset')
          .create(newResult[j]);
        j++;
      }
    }, 1000);
  }) */
// 1PfHXK7vXSMJjzoxN4smLFvJRUH1gY8H2Z
// 1PfHXK7vXSMJjzoxN4smLFvJRUH1gY8H2Z
app.use(express.notFound());
app.use(express.errorHandler({ logger }));
app.hooks(appHooks);

module.exports = app;
